package com.demo.zhlg.service.impl;

import com.demo.zhlg.util.Result;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.demo.zhlg.entity.WalletTransactions;
import com.demo.zhlg.mapper.WalletTransactionsMapper;
import com.demo.zhlg.service.WalletTransactionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import static com.demo.zhlg.entity.table.RatingsTableDef.RATINGS;
import static com.demo.zhlg.entity.table.WalletTransactionsTableDef.WALLET_TRANSACTIONS;

/**
 *  服务层实现。
 *
 * @author lenovo
 * @since 2025-04-26
 */
@Service
public class WalletTransactionsServiceImpl extends ServiceImpl<WalletTransactionsMapper, WalletTransactions> implements WalletTransactionsService {

    @Autowired
    private WalletTransactionsMapper walletTransactionsMapper;

    private int getUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return loginUser.getUser().getUserId();
    }

    @Override
    public Object getTransactions() {
        return Result.success(
                walletTransactionsMapper.selectListByQuery(
                        new QueryWrapper()
                                .where(WALLET_TRANSACTIONS.USER_ID.eq(getUserId()))
                                .orderBy(WALLET_TRANSACTIONS.TRANSACTION_ID, false)
                )
        );
    }
}
